// pages/Pay/Pay.js
const api = require('../../api.js');
const post = api.post;
const getCurrentPageUrlWithArgs = api.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        first: 0,
        newMoney: '10',
        uid: '',
        openid: '',
        list: '',
        shareTz: false, //跳转小程序获取金币
        allowplay: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        var uid = wx.getStorageSync('uid');
        let openid = wx.getStorageSync('weapp_openid');
        if (wx.getStorageSync('allowplay') != 0) {
            that.setData({
                allowplay: true
            })
            that.showMoney()
        }

        that.setData({
            uid: uid,
            openid: openid
        })

    },
    showMoney() {
        let that = this;
        let p = {
            m: 'game_money'
        }
        post(p).then(res => {
            // console.log(res);
            that.setData({
                list: JSON.parse(res.game_money),
                appid: res.appid,
                path: '/pages/integral/integral?third_uid=' + that.data.uid + '&channel=shiguang'
            });
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    Pay_btn(e) {
        let that = this;
        let btnId = e.target.dataset.btn
        that.setData({
            btnId: btnId,
            newMoney: btnId,
            first: btnId
        });
    },
    share() {},
    bindsuccessTz() {
        let that = this;
        that.setData({
            shareTz: true
        })
    },
    TopUp() {
        let that = this;
        wx.request({
            url: 'https://testweapp.whwangdoudou.cn/classic_games/web/lib/weixin/pay.php',
            method: "POST",
            data: {
                uid: that.data.uid,
                money: that.data.newMoney,
                openid: that.data.openid
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success(res) {
                // console.log(res.data.pay_info)
                wx.requestPayment({
                    'timeStamp': res.data.pay_info.timeStamp,
                    'nonceStr': res.data.pay_info.nonceStr,
                    'package': res.data.pay_info.package,
                    'signType': 'MD5',
                    'paySign': res.data.pay_info.paySign,
                    'success': function(res) {
                        // console.log(res);
                    },
                    'fail': function(res) {},
                    'complete': function(res) {}
                })
            }
        });
        // let p = {
        //     uid: that.data.uid,
        //     money: that.data.newMoney,
        //     openid: that.data.openid
        // }
        // post(p).then(res => {
        //     console.log(res);
        // })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        if (that.data.shareTz) {
            wx.navigateTo({
                url: '/pages/palyGame/palyGame',
            });
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        let that = this;
        let url = '/pages/palyGame/palyGame?t_uid=' + that.data.uid
        return {
            title: "时光游戏室",
            path: url,
            imageUrl: "/image/pay.png",
            success: (res) => {

            }
        }
    },
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        var uid = wx.getStorageSync('uid')
        if (!!uid) {
            p.uid = uid
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        // console.log(p);
        post(p).then(res => {})
    }
})